package com.example.app7;

import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RemoteService extends IntentService {

    public static final String TAG = "RemoteService";

    public static final String ACTION_UPDATE_DATA = "action.update.data";
    public static final String EXTRA_DATA = "extra.data";

    private Handler handler;
    private Runnable runnable;

    int record=1;

    int count=0;

    int number;

    boolean status=false;

    //存放数据源


    List<MyData> list = new ArrayList<MyData>();


    public RemoteService() {
        super("");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return binder;
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        list = new ArrayList<MyData>();


        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                // 生成10到20之间的随机数
                int randomNumber = (int) (Math.random() * 11) + 10;
                if (list.size() >=50) {
                    Log.d("list", "run: 生成了"+list.size());
                    status = true;
                } else {

                    Log.d("list", "run: 生成了"+randomNumber);
                    for (int i = count; i <= count+randomNumber; i++) {

                        number=i;

                        MyData myData1 = new MyData("zhaoliying"+i,i);
                        list.add(myData1);
                    }
                    count=number;

                    Log.d("xxf123", "重复的有: 调用后的集合"+list );

                    // 间隔一段时间后再次执行任务
                    handler.postDelayed(this, 5000);

                }

                // 发送广播，并将数据添加到 Intent 中
                Intent updateIntent = new Intent(ACTION_UPDATE_DATA);
                updateIntent.putParcelableArrayListExtra(EXTRA_DATA, new ArrayList<>(list));
                //随机加载的数量
                updateIntent.putExtra("shuliang",randomNumber );
                //第几次执行任务
                updateIntent.putExtra("record", record++);
                //是否要关闭service的状态
                updateIntent.putExtra("status", status);
                sendBroadcast(updateIntent);

            }
        };

        // 第一次执行任务
       // handler.post(runnable);
        //每隔5秒发一次广播
        handler.postDelayed(runnable, 5000);


    }


    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }


    private IRemoteService.Stub binder = new IRemoteService.Stub() {

        @Override
        public int getPid() throws RemoteException {
            return android.os.Process.myPid();
        }

        @Override
        public List<MyData> getMyData() throws RemoteException {
            return list;
        }

        @Override
        public void onMessage(com.example.app7.MyMessage message) throws RemoteException {

        }
    };

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: 正在解除service绑定 ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: 正在销毁service");
    }
}
